package outoforder.leetcode;

import org.junit.Test;

import java.util.HashSet;

/**
 * @author shhjiang_001
 * @create 2020-07-02 23:21
 */
public class Solution567 {
    public boolean checkInclusion(String s1, String s2) {
        char[] s1Chars = s1.toCharArray();
        char[] s2Chars = s2.toCharArray();
        if (s1Chars.length > s2Chars.length) return false;
        int[] count = new int[128];
        int[] windows = new int[128];
        HashSet<Character> set = new HashSet<>();
        for (char ch:s1Chars){
            count[ch]++;
            set.add(ch);
        }
        int le = 0;
        int ri = 0;
        int valid = 0;
        while (ri < s2Chars.length){
            char c = s2Chars[ri];
            ri++;
            if (count[c] != 0){
                windows[c]++;
                if (windows[c] == count[c]){
                    valid++;
                }
            }
            while (ri-le>=s1Chars.length){
                char d = s2Chars[le];
                le++;
                if (valid == set.size()){
                    return true;
                }
                if (count[d] != 0){
                    if (windows[d] == count[d]){
                        valid--;
                    }
                    windows[d]--;
                }
            }
        }
        return false;
    }
    @Test
    public void test(){
        checkInclusion("ab","eidboaoo");
    }
}
